/**
 * Created by xp on 2014/6/23.
 * 站点图组件
 *
 *
 * 用法：
 *1.初始化，其中的url、params等配置项按需配置，如果不需要，请不要配置！
 * $('#id').stationChart({
                url: "",//请求后台url
                params: '',//参数
                autoLoad: false,//初始化的时候是否自动加载数据，如果为true的话就会根据url去请求后台数据
                viewName: 'name'//要显示的字段名称

});
 *
 *
 *2.方法
 *$('#id').stationChart('load',url, params);//请求后台并加载数据
 *$('#id').stationChart('reload');//重新载入数据
 */
(function ($) {

    $.canui('stationChart', {
        options: {
            url: "",
            params: {},
            autoLoad: false,
            viewName: 'stnName'
        },
        _$ul: null,
        _create: function () {
            var _t = this;
            _t.element.addClass('bus-stop-list');
            _t.element.append('<div class="arrow"></div>');
            _t._$ul = $('<ul class="clearfix"></ul>').appendTo(_t.element);
            if (_t.options.autoLoad) {
                _t.load(_t.options.url, _t.options.params);
            }
        },
        _appendNode: function (data) {
            var _t = this;
            _t._$ul.empty();
            $.each(data, function (i, item) {
                var html = '<li>' +
                    '           <div class="num">{0}</div>' +
                    '           <div class="radius"></div>' +
                    '           <div class="name">{1}</div>' +
                    '       </li>';
                var text = item[_t.options.viewName];
                _t._$ul.append(html.format(i+1, text));
            });
        },
        
        //站点间拼接不安全行为数目
        _appendUnsafeNode: function (data, unsafeAr, upDown) {
            var _t = this;
            _t._$ul.empty();
            $.each(data, function (i, item) {
                var html = '<li>' +
                    '           <div class="num">{0}</div>' +
                    '           <div class="radius"></div>' +
                    '           <div class="name">{1}</div>' +
                    '       </li>';
                var text = item[_t.options.viewName];
                //判断拼接故障
                for(var a=0;a<unsafeAr.length;a++){
                	if(unsafeAr[a].PRVS_STN_SN==(i+1) && unsafeAr[a].UP_DN == upDown){
                		html += '<li>' +
                		'           <div class="num"></div>' +
                		'           <div class="radius"></div>' +
                		'           <div class="name">不安全行为数:'+unsafeAr[a].DRV_SCT_BHV_COUNT+'</div>' +
                		'       </li>';
                	}
                }
                
                _t._$ul.append(html.format(i+1, text));
            });
        },
        
        //后两项参数：不安全行为数组，上下行
        load: function (url, params,callback,unsafeAr, upDown) {
            var _t = this;
            if (url && typeof  url !== 'string') {
                params = url;
                url = '';
            }
            _t.options.url = url || _t.options.url;
            _t.options.params = params || _t.options.params;
            if (url) {
                $.ajaxExt({
                    url: url,
                    data: params,
                    success: function (data) {
                        if (!$.isArray(data)) {
                            data = [];
                        }
                        if(unsafeAr && unsafeAr.length>0){
                        	_t._appendUnsafeNode(data,unsafeAr,upDown);
                        }else{
                        	_t._appendNode(data);
                        }
                        if(callback){
                        	callback(data);
                        }
                    }
                })
            }
        },
        reload: function () {
            var _t = this;
            _t.load();
        }
    });

})(jQuery);
